#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
@Project ：Employment_class 
@File    ：04_单进程服务器_非堵塞.py
@IDE     ：PyCharm 
@Author  ：XiaoDengYa
@Date    ：2021/7/6/006 9:57 
"""

from socket import *

g_socketList = []

def main():
    serSocket = socket(AF_INET, SOCK_STREAM)
    serSocket.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
    serSocket.bind(("", 7789))
    serSocket.listen(100)
    serSocket.setblocking(False)
    while True:
        try:
            newClientInfo = serSocket.accept()
        except Exception:
            pass
        else:
            print("一个新的客户端到来:%s"%str(newClientInfo))
            newClientInfo[0].setblocking(False)
            g_socketList.append(newClientInfo)

        for clinetSocket,clinetAddr in g_socketList:
            clinetSocket.setblocking(False)
            try:
                recvData = clinetSocket.recv(1024)
                if recvData:
                    print('recv[%s]:%s'%(str(clinetAddr), recvData))
                else:
                    print('[%s]客户端已经关闭' % str(clinetAddr))
                    clinetSocket.close()
                    g_socketList.remove((clinetSocket,clinetAddr))
            except Exception:
                pass


if __name__ == '__main__':
    main()


